<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/_init_model.php';
	
	class mod_banner extends _init_model
	{
		public $BAN_ID;
		public $BAN_S_DATETIME;		
		public $BAN_E_DATETIME;		
		public $BAN_IMG;
		public $BAN_WIDTH;
		public $BAN_HEIGHT;
		public $BAN_P_ID;
		public $BAN_URL;
		public $BAN_STATE;
		public $BAN_CODE;
		
		function mod_banner(){
			parent::__construct();
						
			$this->tableName = 'banner';		
		}
		
		function get_banner_list($display_length=null, $start_index=0){

			$this->db->select(array('banner.*', 'banner_position.BAN_P_TITLE'));
			$this->db->join('banner_position', 'banner_position.BAN_P_ID = banner.BAN_P_ID', 'left');
			$this->db->order_by('BAN_ID', 'DESC');										
			//$query = $this->db->get($this->tableName, $display_length, $start_index);
			$query = $this->db->get($this->tableName);			
				
			$return = $query->result_array();			
			if (count($return) > 0){				
				$this->load->library('define/directory_path');
				$this->load->library('custom_datetime');
				
				for ($i=0; $i<count($return); $i++){
					if (empty($return[$i]['BAN_CODE']) && !empty($return[$i]['BAN_IMG'])){					
						$return[$i]['BAN_IMG'] = $this->directory_path->img_banner_path . $return[$i]['BAN_IMG'];					
						$return[$i]['BAN_REMAIN'] = $this->custom_datetime->date_diff($return[$i]['BAN_S_DATETIME'], $return[$i]['BAN_E_DATETIME']);
					} else {
						$return[$i]['BAN_IMG'] = $this->directory_path->img_codeblock;
						$return[$i]['BAN_REMAIN'] = 'ไม่จำกัด';
					}
				}
			}
			
			return $return;
		}
		function get_banner_homepage_zone(){
			$this->db->where('banner.BAN_P_ID', 1);
			$this->db->where('banner.BAN_STATE', 1);
			return $this->get_banner_list();
		}
		function get_banner_article_view_zone(){
			$this->db->where('banner.BAN_P_ID', 2);
			$this->db->where('banner.BAN_STATE', 1);
			return $this->get_banner_list();
		}
		function get_banner_product_view_zone(){
			$this->db->where('banner.BAN_P_ID', 2);
			$this->db->where('banner.BAN_STATE', 1);
			return $this->get_banner_list();
		}
		function get_single_banner($id){
			$this->db->where('BAN_ID', $id);
			$this->db->join('banner_position', 'banner.BAN_P_ID = banner_position.BAN_P_ID','left');
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0){
				$row = $query->row_array(0);
				$this->load->library('define/directory_path');
				$this->load->library('custom_datetime');
				
				$row['BAN_IMG'] = $this->directory_path->img_banner_path . $row['BAN_IMG'];
				$row['BAN_REMAIN'] = $this->custom_datetime->date_diff($row['BAN_S_DATETIME'], $row['BAN_E_DATETIME']);
				
				return $row;
			} else 
				return null;
		}
		
		function set_banner_state($banner_id, $state){
			$this->db->where('BAN_ID', $banner_id);
			if ($state)
				$state = 1;
			else
				$state = 0;
			return $this->db->update($this->tableName, array('BAN_STATE' => $state));
		}
		
		function add_banner(){
			
			$this->load->library('custom_datetime');
			$this->BAN_S_DATETIME = $this->custom_datetime->current_datetime();						
			
			$data = array('BAN_S_DATETIME' => $this->BAN_S_DATETIME,							
							'BAN_E_DATETIME' => date('Y-m-d H:i:s', strtotime($this->BAN_E_DATETIME . ' + 1 days')),
							'BAN_IMG' => $this->BAN_IMG,
							'BAN_WIDTH' => $this->BAN_WIDTH,
							'BAN_HEIGHT' => $this->BAN_HEIGHT,
							'BAN_P_ID' => $this->BAN_P_ID,
							'BAN_STATE' => false,
							'BAN_URL' => $this->BAN_URL);
			return $this->db->insert($this->tableName, $data);
		}
		function add_codeblock(){
			$this->load->library('custom_datetime');
			$this->BAN_S_DATETIME = $this->custom_datetime->current_datetime();			
			
			$data = array('BAN_S_DATETIME' => $this->BAN_S_DATETIME,
							'BAN_E_DATETIME' => date('Y-m-d H:i:s', strtotime($this->BAN_E_DATETIME . ' + 1 days')),
							'BAN_P_ID' => $this->BAN_P_ID,
							'BAN_STATE' => false,
							'BAN_CODE' => $this->BAN_CODE);
			return $this->db->insert($this->tableName, $data);
		}
		function edit_banner($banner_id){
			$data = array();
			
			if (!empty($this->BAN_S_DATETIME))
				$data['BAN_S_DATETIME'] = date('Y-m-d H:is:s', strtotime($this->BAN_S_DATETIME));
			if (!empty($this->BAN_E_DATETIME))
				$data['BAN_E_DATETIME'] = date('Y-m-d H:i:s', strtotime($this->BAN_E_DATETIME));
			if (!empty($this->BAN_WIDTH))
				$data['BAN_WIDTH'] = $this->BAN_WIDTH;
			if (!empty($this->BAN_HEIGHT))
				$data['BAN_HEIGHT'] = $this->BAN_HEIGHT;
			if (!empty($this->BAN_P_ID))
				$data['BAN_P_ID'] = $this->BAN_P_ID;
			if (!empty($this->BAN_STATE))
				$data['BAN_STATE'] = $this->BAN_STATE;
			if (!empty($this->BAN_URL))
				$data['BAN_URL'] = $this->BAN_URL;
			
			$this->db->where('BAN_ID', $banner_id);
			return $this->db->update($this->tableName, $data);
		}
		function edit_codeblock($banner_id){
			$data = array();
			
			if (!empty($this->BAN_S_DATETIME))
				$data['BAN_S_DATETIME'] = date('Y-m-d H:is:s', strtotime($this->BAN_S_DATETIME));
			if (!empty($this->BAN_E_DATETIME))
				$data['BAN_E_DATETIME'] = date('Y-m-d H:i:s', strtotime($this->BAN_E_DATETIME . ' + 1 days'));			
			if (!empty($this->BAN_P_ID))
				$data['BAN_P_ID'] = $this->BAN_P_ID;
			if (!empty($this->BAN_STATE))
				$data['BAN_STATE'] = $this->BAN_STATE;
			if (!empty($this->BAN_CODE))
				$data['BAN_CODE'] = $this->BAN_CODE;
			
			$this->db->where('BAN_ID', $banner_id);
			return $this->db->update($this->tableName, $data);
		}
		function remove($banner_id){
			// remove image			
			$this->db->select('BAN_IMG');
			$this->db->where('BAN_ID', $banner_id);
			$query = $this->db->get($this->tableName);
			if ($query->num_rows() > 0){
				if (!empty($query->row(0)->BAN_IMG)){
					$this->load->library('define/directory_path');
					unlink($this->directory_path->img_upload_banner_path . $query->row(0)->BAN_IMG);
				}
			}			
			$this->db->flush_cache();
			
			// remove data
			$this->db->where('BAN_ID', $banner_id);
			return $this->db->delete($this->tableName);
		}
	}
?>